package com.bolt.report.business.modules.field.controller;

import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.report.business.modules.field.dto.ReportDbFieldDTO;
import com.bolt.report.business.modules.field.dto.ReportDbFieldQueryCriteria;
import com.bolt.report.business.modules.field.dto.converter.ReportDbFieldConverter;
import com.bolt.report.business.modules.field.entity.ReportDbFieldEntity;
import com.bolt.report.business.modules.field.service.ReportDbFieldService;
import com.bolt.support.spring.jpa.specification.QueryHelp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2021-11-17 11:40:21
 **/
@RestController
@RequestMapping("report/design/reportDbField")
@RequiredArgsConstructor
@Api(tags = "")
public class ReportDbFieldController {

    private final ReportDbFieldService reportDbFieldService;

    /**
     * 列表分页
     *
     * @return
     */
    @PostMapping(value = "/page")
    @ApiOperation(notes = "列表分页列表分页", httpMethod = "GET", value = "列表分页")
    public ResultMessage list(Pageable pageable, ReportDbFieldQueryCriteria queryCriteria) {
        PageInfo<ReportDbFieldEntity> pageInfo = reportDbFieldService.findPage(pageable, (root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, queryCriteria, criteriaBuilder));
        return Results.success(pageInfo.map(K -> ReportDbFieldConverter.toDTO(K)));
    }

    /**
     * 根据ID操作
     */
    @GetMapping("/get/{id}")
    @ApiOperation(notes = "根据ID查询", httpMethod = "GET", value = "根据ID查询")
    public ResultMessage<ReportDbFieldDTO> get(@PathVariable String id) {
        ReportDbFieldEntity reportDbFieldEntity = reportDbFieldService.findOne(id).get();
        return Results.success(ReportDbFieldConverter.toDTO(reportDbFieldEntity));
    }

    /**
     * 新增操作
     *
     * @param dao 表单数据
     * @return
     */
    @PostMapping
    @ApiOperation(notes = "新增", httpMethod = "POST", value = "新增")
    public ResultMessage<Void> create(@Validated @RequestBody ReportDbFieldDTO dao) {
        reportDbFieldService.saveOrUpdate(dao);
        return Results.success();
    }

    /**
     * 编辑操作
     *
     * @param dao 表单数据
     * @return
     */
    @PutMapping
    @ApiOperation(notes = "编辑", httpMethod = "PUT", value = "编辑")
    public ResultMessage<Void> update(@Validated(ReportDbFieldDTO.Update.class) @RequestBody ReportDbFieldDTO dao) {
        reportDbFieldService.saveOrUpdate(dao);
        return Results.success();
    }

    /**
     * 删除操作
     */
    @DeleteMapping
    @ApiOperation(notes = "根据ID删除", httpMethod = "DELETE", value = "根据ID删除")
    public ResultMessage<Void> doDelete(@RequestBody Set<String> ids) {
        for (String id : ids) {
            reportDbFieldService.deleteById(id);
        }
        return Results.success();
    }


    /**
     * 根据dataSetId查询报表字段信息
     */
    @GetMapping("/list/{dataSetId}")
    @ApiOperation(notes = "根据dataSetId查询报表字段信息", httpMethod = "GET", value = "根据dataSetId查询报表字段信息")
    public ResultMessage<Set<ReportDbFieldDTO>> list(@PathVariable String dataSetId) {
        List<ReportDbFieldEntity> dbFieldEntities = reportDbFieldService.findAll(k -> k.lambda().eq(ReportDbFieldEntity::getReportDbId, dataSetId).build());
        return Results.success(ReportDbFieldConverter.toDTO(dbFieldEntities));
    }
}